<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：CooperateAgency.php
 * @类功能: 合作机构管理
 * @开发者: gys
 * @开发时间： 15-10-22
 * @版本：version 1.0
 */
namespace app\admin\controller;

use app\common\cache\common\ICooperateAgencyCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class CooperateAgency extends AdminBaseController {
    /**
     * @功能：合作机构列表
     * @开发者：gys
     */
    public function cooperate_agency_list(){
        $agency_name = IRequest::get("agency_name");
        $website_url = IRequest::get("website_url");
        $is_recommend = IRequest::get("is_recommend");
        $remark = IRequest::get("remark");

        $cooperate_agency_where = [];
        if(!empty($agency_name)) $cooperate_agency_where['agency_name'] = ['like',"%{$agency_name}%"];
        if(!empty($website_url)) $cooperate_agency_where['website_url'] = ['like',"%{$website_url}%"];
        if(!empty($is_recommend)) $cooperate_agency_where['is_recommend'] = $is_recommend;
        if(!empty($remark)) $cooperate_agency_where['remark'] = ['like',"%{$remark}%"];

        //获取合作机构列表
        $cooperate_agency_list = IDb::getInstance("cooperate_agency")->setDbWhere($cooperate_agency_where)->setDbOrder('order_by desc,cooperate_agency_id desc')->sel();

        //模板渲染
        $this->assign('cooperate_agency_list',$cooperate_agency_list);
        $this->assign('agency_name',$agency_name);
        $this->assign('website_url',$website_url);
        $this->assign('is_recommend',$is_recommend);
        $this->assign('remark',$remark);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：合作机构添加
     * @开发者：gys
     */
    public function cooperate_agency_add(){
        //渲染模板输出
        return view();
    }

    /**
     * @功能：合作机构编辑
     * @开发者：gys
     */
    public function cooperate_agency_edit(){
        //接收参数
        $cooperate_agency_id = IRequest::get("cooperate_agency_id",IRequest::NOT_EMPTY,"合作机构编号不能为空！");

        //实例化对象
        $cooperate_agency_where['cooperate_agency_id'] = $cooperate_agency_id;
        $cooperate_agency_info = IDb::getInstance("cooperate_agency")->setDbWhere($cooperate_agency_where)->row();

        //模板渲染
        $this->assign('cooperate_agency_id',$cooperate_agency_id);
        $this->assign('cooperate_agency_info',$cooperate_agency_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：合作机构编辑提交
     * @开发者：gys
     */
    public function cooperate_agency_submit(){
        //接收参数
        $cooperate_agency_id = IRequest::get("cooperate_agency_id");
        $agency_name = IRequest::get("agency_name",IRequest::NOT_EMPTY,"合作机构名称不能为空！");
        $img_url = IRequest::get("img_url",IRequest::NOT_EMPTY,"合作机构图片不能为空！");
        $website_url = IRequest::get("website_url");
        $order_by = IRequest::get("order_by");
        $is_recommend = IRequest::get("is_recommend");
        $remark = IRequest::get("remark");

        $cooperate_agency_data['agency_name'] = $agency_name;
        $cooperate_agency_data['img_url'] = $img_url;
        $cooperate_agency_data['website_url'] = $website_url;
        $cooperate_agency_data['order_by'] = $order_by;
        $cooperate_agency_data['is_recommend'] = $is_recommend;
        $cooperate_agency_data['remark'] = $remark;

        //判断是修改还是添加
        if(empty($cooperate_agency_id)) {
            //添加合作机构编号
            $cooperate_agency_data['create_admin'] = get_login_admin_id();
            $cooperate_agency_data['create_time'] = get_date_time();
            $cooperate_agency_add = IDb::getInstance("cooperate_agency")->setDbData($cooperate_agency_data)->add();
            if($cooperate_agency_add === false){
                error('添加失败');
            }
        }else{
            //修改合作机构
            $cooperate_agency_data['update_admin'] = get_login_admin_id();
            $cooperate_agency_data['update_time'] = get_date_time();
            $cooperate_agency_where['cooperate_agency_id'] = array("eq",$cooperate_agency_id);
            $cooperate_agency_upd = IDb::getInstance("cooperate_agency")->setDbWhere($cooperate_agency_where)->setDbData($cooperate_agency_data)->upd();
            if($cooperate_agency_upd === false){
                error('修改失败！');
            }
        }

        //清除缓存
        ICooperateAgencyCache::deleteList();

        //画面跳转
        $this->success('操作成功',Url('cooperate_agency_list'));
    }

    /**
     * @功能：删除合作机构
     * @开发者：gys
     */
    public function cooperate_agency_del(){
        //接收参数
        $cooperate_agency_id= IRequest::get("cooperate_agency_id",IRequest::NOT_EMPTY,"合作机构编号不能为空！");

        $cooperate_agency_where['cooperate_agency_id'] = array("eq",$cooperate_agency_id);
        $data["is_delete"] = IsDelete::Yes;
        //删除合作机构
        $agency_upd = IDb::getInstance("cooperate_agency")->setDbWhere($cooperate_agency_where)->setDbData($data)->upd();
        if ($agency_upd === false) {
            error("删除合作机构失败！");
        }

        //清除缓存
        ICooperateAgencyCache::deleteList();

        //画面跳转
        $this->success('操作成功',Url('cooperate_agency_list'));
    }

    /**
     * @功能：还原合作机构
     * @开发者：gys
     */
    public function cooperate_agency_reset(){
        //接收参数
        $cooperate_agency_id= IRequest::get("cooperate_agency_id",IRequest::NOT_EMPTY,"合作机构编号不能为空！");

        $cooperate_agency_where['cooperate_agency_id'] = array("eq",$cooperate_agency_id);
        $data["is_delete"] = IsDelete::No;
        //还原合作机构
        $agency_upd = IDb::getInstance("cooperate_agency")->setDbWhere($cooperate_agency_where)->setDbData($data)->upd();
        if ($agency_upd === false) {
            error("还原合作机构失败！");
        }

        //清除缓存
        ICooperateAgencyCache::deleteList();

        //画面跳转
        $this->success('操作成功',Url('cooperate_agency_list'));
    }
}
